const fs = require('fs');
const path = require('path');
const https = require('https');
const cheerio = require('cheerio');
const bcrypt = require('bcrypt');
module.exports = {
	prefixInteger(num, n) {
		return (Array(n).join(0) + num).slice(-n);
	},
	getChapter(url, callback) {
		https.get(url, function(response) {
			let chunks = []
			let size = 0;
			response.on('data', function(chunk) {
				chunks.push(chunk);
				size += chunk.length;
			});
			response.on('end', function() {
				let data = Buffer.concat(chunks, size);
				let html = data.toString();
				let cnt = cheerio.load(html);
				callback(cnt);
			});
		})
	},
	isBookNo(str) {
		return /^BK\d{10}$/.test(str);
	},
	//密码加密
	hashPass(pwd) {
		let pass;
		bcrypt.hash(pwd, 10, (err, hash) => {
			pass = hash;
		});
		return pass;
	},
	//加密密码对比
	comparePass(pwd, hash) {
		let flag;
		bcrypt.compare(pwd, hash, (err, res) => {
			flag = res;
		})
		return flag;
	}
}